import logging

import pytest

from resource.pom.gallery import Gallery
from utils.manager import MatchRules


@pytest.fixture(scope='function', autouse=True)
def func_setup_teardown(device):
    logging.info('------用例前置：预置图片资源------')
    app = Gallery
    device.mediatool_delete_all()
    device.hdc_file_send(app.photo1, app.photo_dir)
    device.mediatool_send(app.photo_dir)
    yield
    logging.info('------用例后置：删除预置资源------')
    device.mediatool_delete_all()


class Test:
    app = Gallery

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('预置条件：烧写目标版本后，正常进入桌面-图库应用，已新建相册')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.assert_current_window(self.app.window_name)
        device.refresh_layout()
        # 照片
        tab_photos = device.get_element_by_key('TabPhotos')
        # 相册
        tab_albums = device.get_element_by_key('TabAlbums')
        device.click_element(tab_albums)
        device.refresh_layout()
        device.click_element(device.get_element_by_key('ActionBarButtonNew'))
        device.refresh_layout()
        new_album_name = device.get_element_by_key('NewAlbumDialogTextInput').get('text')
        device.click_element(device.get_element_by_text('确定'))
        device.go_back()

        logging.info('步骤1.选择一个已新建的相册长按进入选项')
        device.refresh_layout()
        device.long_click_element(device.get_element_by_key('Album_1'))
        logging.info('预期结果1.可以正常进入到相册选项页面')
        device.refresh_layout()
        device.assert_text_exist('重命名')
        device.assert_text_exist('删除')

        logging.info('步骤2.点击删除按钮')
        device.click_element(device.get_element_by_key('ToolBarButtonDelete'))
        logging.info('预期结果2.正常弹出删除确认弹框，弹框显示“是否删除此相册(包括x张照片，y个视频)？您可在30天内从最近删除中恢复或彻底删除”的提示')
        device.refresh_layout()
        device.assert_type_exist('Dialog')
        device.assert_text_exist('是否删除此相册', rule=MatchRules.Contain)
        device.assert_text_exist('取消')
        device.assert_text_exist('删除')

        logging.info('步骤3.点击删除')
        device.click_element(device.get_element_by_key('DeleteDialogConfirmButton'))
        logging.info('预期结果3.相册已被删除')
        device.refresh_layout()
        device.assert_text_exist(new_album_name, expect=False)
